package com.dk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.mapper.PersonRoleMapper;
import com.dk.mapper.TPersonMapper;
import com.dk.mapper.TRoleMapper;
import com.dk.pojo.PersonRoleExample;
import com.dk.pojo.TPerson;
import com.dk.service.personservice;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
@Service
@org.springframework.stereotype.Service
public class personserviceImpl implements personservice {

    @Autowired
    private TPersonMapper tPersonMapper;
    @Autowired
    private PersonRoleMapper personRoleMapper;

    @Override
    public List<TPerson> selectgetinfo() {
        List<TPerson> list = tPersonMapper.selectByExample(null);
        return list;
    }

    @Override
    public boolean insertnraandactor(TPerson tPerson, Integer[] aid) {
        tPerson.setPwd("1234");
        Integer i = tPersonMapper.insertreturnnid(tPerson);
        Integer nid = tPerson.getPid();
        int ii = personRoleMapper.insertBatch(nid,aid);

        return true;
    }

    @Override
    public TPerson selectbynid(Integer pid) {
        return tPersonMapper.selectByPrimaryKey(pid);
    }

    @Override
    public boolean updateNra(TPerson tPerson, Integer[] roleId) {
        tPersonMapper.updateByPrimaryKeySelective(tPerson);
        //删除角色信息
        PersonRoleExample urm = new PersonRoleExample();
        PersonRoleExample.Criteria criteria = urm.createCriteria();
        criteria.andPersonIdEqualTo(tPerson.getPid());
        personRoleMapper.deleteByExample(urm);
        //批量增加中间表
        int i2 = personRoleMapper.insertBatch(tPerson.getPid(),roleId);
        return true;
    }

    @Override
    public boolean deleteBynid(Integer pid) {
        int i = tPersonMapper.deleteByPrimaryKey(pid);
        PersonRoleExample nae = new PersonRoleExample();
        PersonRoleExample.Criteria criteria = nae.createCriteria();
        criteria.andPersonIdEqualTo(pid);
        int i1 = personRoleMapper.deleteByExample(nae);
        return true;
    }

    @Override
    public TPerson selectInfo(TPerson tPerson) {
       return tPersonMapper.selectbyname(tPerson.getPname());
    }
}
